"""
图片及图片的处理
"""
from os import path
from .draw import load_img
from .draw import draw_img
from src.event import mouse

# 图片资源的所在目录的绝对路径
IMAGES_ROOT_DIR = path.join(path.dirname(__file__), '../../resources/images')


def gen_img_path(img_relative_path):
    """
    根据图片相对于图片资源根目录的相对路径生成图片的绝对路径
    :param img_relative_path: 图片相对于图片资源根目录的相对路径
    :return: 图片的绝对路径
    """
    return path.join(IMAGES_ROOT_DIR, img_relative_path)


# 图片资源的绝对路径
# 开始游戏界面图片
START_BG = gen_img_path('./start/bg.jpg')
START_LOGO = gen_img_path('./start/logo.png')
START_BTN_BAR_DIRT = gen_img_path('./start/btn_bar_dirt.png')
START_BTN_BAR_GRASS = gen_img_path('./start/btn_bar_grass.png')
START_BTN_BAR_GRASS_ROLL = gen_img_path('./start/btn_bar_grass_roll.png')
# 游戏菜单界面图片
MAIN_MENU_BG = gen_img_path('./menu/bg.png')
MAIN_MENU_ITEM_START_ADVENTURE_ACTIVE = gen_img_path('./menu/menu_item_start_adventure_active.png')
MAIN_MENU_ITEM_START_ADVENTURE = gen_img_path('./menu/menu_item_start_adventure.png')
SETTING_OPTIONS = gen_img_path('./menu/setting_options.png')
SETTING_OPTIONS_ACTIVE = gen_img_path('./menu/setting_options_active.png')
SETTING_HELP = gen_img_path('./menu/setting_help.png')
SETTING_HELP_ACTIVE = gen_img_path('./menu/setting_help_active.png')
SETTING_QUIT = gen_img_path('./menu/setting_quit.png')
SETTING_QUIT_ACTIVE = gen_img_path('./menu/setting_quit_active.png')
# 游戏地图图片
MAP_BG_GRASS_FULL = gen_img_path('./map/bg_grass_full.jpg')
# 植物相关图片
SEED_BANK = gen_img_path('./plants/seed/seed_bank.png')
CARD_SUNFLOWER = gen_img_path('./plants/card/card_sunflower.png')

# 图片对象
# 开始游戏界面图片
start_bg = None
start_logo = None
start_btn_bar_dirt = None
start_btn_bar_grass = None
start_btn_bar_grass_roll = None
# 游戏菜单界面图片
main_menu_bg = None
main_menu_item_start_adventure_active = None
main_menu_item_start_adventure = None
setting_options = None
setting_options_active = None
setting_help = None
setting_help_active = None
setting_quit = None
setting_quit_active = None
# 游戏地图图片
map_bg_grass_full = None
# 植物相关图片
seed_bank = None
card_sunflower = None


def load_img_all():
    """
    加载全部图片
    :return:
    """
    # 开始游戏界面图片
    global start_bg
    global start_logo
    global start_btn_bar_dirt
    global start_btn_bar_grass
    global start_btn_bar_grass_roll
    start_bg = load_img(START_BG)
    start_logo = load_img(START_LOGO)
    start_btn_bar_dirt = load_img(START_BTN_BAR_DIRT)
    start_btn_bar_grass = load_img(START_BTN_BAR_GRASS)
    start_btn_bar_grass_roll = load_img(START_BTN_BAR_GRASS_ROLL)
    # 游戏菜单界面图片
    global main_menu_bg
    global main_menu_item_start_adventure_active
    global main_menu_item_start_adventure
    global setting_options
    global setting_options_active
    global setting_help
    global setting_help_active
    global setting_quit
    global setting_quit_active
    main_menu_bg = load_img(MAIN_MENU_BG)
    main_menu_item_start_adventure_active = load_img(MAIN_MENU_ITEM_START_ADVENTURE_ACTIVE)
    main_menu_item_start_adventure = load_img(MAIN_MENU_ITEM_START_ADVENTURE)
    setting_options = load_img(SETTING_OPTIONS)
    setting_options_active = load_img(SETTING_OPTIONS_ACTIVE)
    setting_help = load_img(SETTING_HELP)
    setting_help_active = load_img(SETTING_HELP_ACTIVE)
    setting_options = load_img(SETTING_OPTIONS)
    setting_options_active = load_img(SETTING_OPTIONS_ACTIVE)
    setting_quit = load_img(SETTING_QUIT)
    setting_quit_active = load_img(SETTING_QUIT_ACTIVE)
    # 游戏地图图片
    global map_bg_grass_full
    map_bg_grass_full = load_img(MAP_BG_GRASS_FULL)
    # 植物相关图片
    global seed_bank
    global card_sunflower
    seed_bank = load_img(SEED_BANK)
    card_sunflower = load_img(CARD_SUNFLOWER)


class Image:
    """
    图片类
    """
    # 图片对象
    __obj = None
    # 图片 Rect 对象
    __rect = None
    # 是否为激活状态
    __is_active = False
    # 图片
    __img = None
    # 激活状态下的图片
    __img_active = None
    # 图片要绘制在窗口中的位置
    __position = (0, 0)
    # 图片绘制在窗口中是否水平居中
    __is_h_center = False
    # 图片绘制在窗口中是否垂直居中
    __is_v_center = False
    # 图片要绘制在窗口中的大小
    __size = None
    # 图片是否为透明背景的图片
    __is_transparent = False
    # 点击事件处理函数
    __mousedown_handler = None
    # 鼠标悬浮事件处理函数
    __mousemotion_handler = None

    def __init__(self, is_active=False, img=None, img_active=None, position=(0, 0), is_h_center=False,
                 is_v_center=False, size=None, is_transparent=False):
        self.__is_active = is_active
        self.__img = img
        self.__img_active = img_active
        self.__position = position
        self.__is_h_center = is_h_center
        self.__is_v_center = is_v_center
        self.__size = size
        self.__is_transparent = is_transparent
        self.__mousemotion_handler = mousemotion_default_handler

    def draw(self):
        """
        在窗口中绘制图片
        :return:
        """
        img = self.__img
        if self.__is_active:
            img = self.__img_active
        img_tuple = draw_img(
            img, self.__position, self.__size, self.__is_h_center, self.__is_v_center, self.__is_transparent
        )
        self.__obj = img_tuple[0]
        self.__rect = img_tuple[1]

    def add_mousedown_listen(self, mousedown_handler=None):
        """
        为图片对象添加鼠标点击事件监听
        :param mousedown_handler: 点击事件处理函数
        :return:
        """
        if self.__obj is None or self.__rect is None:
            self.draw()
        if mousedown_handler is not None:
            self.__mousedown_handler = mousedown_handler
        mouse.obj_dict_listen_mousedown[self] = self.__mousedown_handler

    def add_mousemotion_listen(self, mousemotion_handler=None):
        """
        为图片对象添加鼠标悬浮事件监听
        :param mousemotion_handler: 鼠标悬浮事件处理函数
        :return:
        """
        if self.__obj is None or self.__rect is None:
            self.draw()
        if mousemotion_handler is not None:
            self.__mousemotion_handler = mousemotion_handler
        mouse.obj_dict_listen_mousemotion[self] = self.__mousemotion_handler

    def get_obj(self):
        return self.__obj

    def get_rect(self):
        return self.__rect

    def get_is_active(self):
        return self.__is_active

    def set_is_active(self, is_active):
        self.__is_active = is_active


def mousemotion_default_handler(is_mousemotion, key, event):
    """
    鼠标悬浮事件默认处理函数
    :param is_mousemotion: 鼠标是否悬浮在当前对象上
    :param key: 当前需要监听鼠标悬浮事件的字典集合中匹配的 key（保存到字典中时以对象为 key）
    :param event: 事件对象
    :return:
    """
    if is_mousemotion:
        mouse.hand_mouse()
        key.set_is_active(True)
    else:
        mouse.arrow_mouse()
        key.set_is_active(False)